<?php
/*********************************************************************\
* This File is a part of BH-PANEL (Breizh-Heberg Panel v2.1)
* Copyright (C) 2008-2009 the BH Developpers : Vincent Giersch <mail@vincordi.fr>, Cyprien Laleau <fanning.fr@gmail.com> and Edwin Cabiten <marmottes44@hotmail.fr>
* See file AUTHORS to get more informations

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
/*********************************************************************/


class bh_doms_gest {
	private $session;
	private $soap;
	
	private $login = 0000;
	private $password = "";
	public function __construct() {
		$this->soap = new SoapClient(NULL,
			array('location' => "https://api.free.org/apis.cgi",
					'uri' => "https://api.free.org/apis.cgi",
					'encoding'=>'UTF-8'
					));
	}	
	
	private function error($error) {
		$tpl = new gagatemplate('');
		$tpl->assign(array('fault' => $error));
		$tpl->parse('global-soaperror.html');
		exit();
	}
	private function contact_pass() {
		$chaine = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		$nb_caract = 8;
		$pass = "";
		for($u = 1; $u <= $nb_caract; $u++) {
		    $nb = strlen($chaine);
		    $nb = mt_rand(0,($nb-1));
		    $pass.=$chaine[$nb];
		}
		return $pass;
	}
	
	public function deconnect() {
		return true;
	}
	
	public function codes_pays() {
		try {
			return $this->soap->api_paysid($this->login, $this->password);
		}
		catch(SoapFault $fault) {
			$this->error($fault);
		}
	}

	public function infos_dom($domaine) {

	}
	
	
	public function contact_create($array) {
		$contact_pass = $this->contact_pass();
		if($array['naissance'] == "") {
			$array['naissance'] = date("d-m-Y",$array['UNIX_TIMESTAMP(users.u_naissance)']);
			$array['naissance_cp'] = intval($array['u_cp']);
			$array['naissance_ville'] = strtoupper($array['u_ville']);
			$array['naissance_pays'] = $array['u_pays'];
		}
		
		$contact = array(
			"passwd" => $contact_pass,
	        "email" => $array['u_email'],
	        "hash_email" => "yes",
	        "formejuridique" => "Particulier",
	        "civilite" => $array['u_civilite'],
	        "nom" => $array['u_nom'],
	        "prenom" => $array['u_prenom'],
	        "societe" => "",
	        "adresse1" => $array['u_adresse'],
	        "adresse2" => "",
	        "cp" => intval($array['u_cp']),
	        "ville" => strtoupper($array['u_ville']),
	        "region_id" => "",
	        "pays_id" => $array['u_pays'],
	        "telephone" => $array['u_tel'],
	        "telecopie" => "",
	        "numero_tva" => "",
	        "siret" => "",
	        "lang" => "fr",
	        "revente" => "no",
	 
			// Extensions (utile uniquement pour lenregistrement de .FR)
	 
			// choix de la methode d'authentification par l'AFNIC
	        "dotfr_mode" => "PARTICULIER",         // enum('MARQUE','SIRET','PARTICULIER','ASSOCIATION')
	 
	        "dotfr_marque" => "",       // une marque de l'INPI ou OHMI
	 
			// pour un particulier
	        "dotfr_ne_le" => $array['naissance'],        // date de naissance sous la forme: (JJ-MM-AAAA)
	        "dotfr_ne_cp" => $array['naissance_cp'],        // code postal du lieu de naissance
	        "dotfr_ne_ville" => $array['naissance_ville'],     // ville du lieu de naissance
	        "dotfr_ne_pays_id" => $array['naissance_pays'],  // pays du lieu de naissance - voir api_paysid();
	 
			// pour une association loi 1901
	        "dotfr_publication_jo" => "", // date de publication au journal officiel sous la forme: (JJ-MM-AAAA)
	        "dotfr_publication_jo_page" => "", // numero de page de la publication au journal officiel
        );

		try {
			$return_serv = $this->soap->contact_create($this->login,$this->password,$contact);
		}
		catch(SoapFault $fault) {
			$this->error($fault);
		}
		if($return_serv['retcode'] == 1) {
			$array_return = array(
				"etat" => 1,
				"reg_contact_id" => $return_serv['retfields']['id'],
				"reg_nic" => $return_serv['retfields']['handle'],
				"reg_pass" => $contact_pass);
		}
		else {
			$array_return = array(
				"etat" => $return_serv['retval']
				);
		}
		return $array_return;
	}
	
	public function contact_list() {
		return $this->soap->contact_list($this->login, $this->password);
	}
	
	public function dom_dispo($domaine) {
		$domaine = preg_replace('#([^a-zA-Z0-9\-.])+#i', '', strtolower($domaine));
		$extension = explode(".", $domaine);
		if($extension[1] == "be") {
			$check_return = shell_exec('whois -h whois.dns.be -p 4343 "get 1.0 '.$extension[0].'"');
			$regex_return_free = "Status: FREE";
		}
		elseif($extension[1] == "eu") {
			$check_return = shell_exec('whois -h das.eu -p 4343 "get 1.0 '.$extension[0].'"');
			$regex_return_free = "Status: FREE";
		}
		else {
			$check_return = shell_exec('whois -h das.free.org -p 4343 "get 1.0 '.$domaine.'"');
			$regex_return_free = "Status: Free";
		}
		if(eregi($regex_return_free,$check_return)) {
			return 1;
		}
		else {
			return 2;
		}		
	}
	
	public function dom_create($domaine, $client) {
		$new_dom = array(
			"domain"   => $domaine,
			"period"   => 1,
	        "owner_id" => $client,
	        "admin_id" => $this->login,
	        "tech_id"  => $this->login,
	        "bill_id"  => $this->login,
	        "ns0" => "ns0.breizh-heberg.net",
	        "ns1" => "ns1.breizh-heberg.net",
			"service"  => "dns",
			"ip_dns_master"  => "",
			"ipns0"  => "",
			"ipns1"  => "",
			"registry_status" => 64,
		);
		$return_serv = $this->soap->domain_create($this->login,$this->password,$new_dom);
		if($return_serv['retcode'] == 1) {
			$array_return = array(
				"etat" => 1,
				"tid" => $return_serv['retfields']['tid'],
				"authinfo" => $return_serv['retfields']['authinfo'],
			);
		}
		else {
			$array_return = array(
				"etat" => $return_serv['retval']
				);
		}
		return $array_return;
	}
	
	public function dom_update($domaine, $infos) {
		try {
			$i_domaine = array(
		        "domain"   => $domaine,  // nom de domaine
		        "period"   => 1,        // enregistrement pour 1 an
		        "owner_id" => $infos['reg_contact_id'],      // contact ID cree dans le meme exemple
		        "admin_id" => $this->login, // Contact Admin = revendeur 
		        "tech_id"  => $this->login, // Contact Technique = revendeur
		        "bill_id"  => $this->login, // Contact Facturation = revendeur
		        "service"  => "dns",    // dns ou dnsslave
		        "ip_dns_master"  => "", // si dnsslave => fournir l'IP du master
		        "ns0" => $infos['dns1'],
		        "ns1" => $infos['dns2'],
		        "ns2" => $infos['dns3'],
		        "ns3" => $infos['dns4'],
		        "ipns0"  => "",
		        "ipns1"  => "",
		        "registry_status" => $infos['registry_status'],     // clientTransferProhibited on/off
		        );

			$infos['domain'] = $domaine;
			$return =  $this->soap->domain_update($this->login,$this->password,$domaine,$i_domaine);
		}
		catch(SoapFault $fault) {
			$this->error($fault);
		}
		return $return['retfields']['tid'];
	}
	
	
	public function dom_infos($domaine) {
		try {
			$infos = $this->soap->domain_info($this->login,$this->password,$domaine);
		}
		catch(SoapFault $fault) {
			$this->error($fault);
		}
		$array =  array(
			"create" => $infos['retfields']['registrar_creation'],
			"expire" => $infos['retfields']['registrar_expiration'],
			"dns1" => $infos['retfields']['ns0'],
			"dns2" => $infos['retfields']['ns1'],
			"dns3" => $infos['retfields']['ns2'],
			"dns4" => $infos['retfields']['ns3'],
			"authinfo" => $infos['retfields']['authinfo']
		);
		return $array;
	}
	
	public function dom_lock($domaine) {
		try {
			$update['registry_status'] = 64;
			return $this->dom_update($domaine,$update);
		}
		catch(SoapFault $fault) {
			$this->error($fault);
		}
	}
	
	public function dom_unlock($domaine) {
		try {
			$update['registry_status'] = 0;
			return $this->dom_update($domaine,$update);
		}
		catch(SoapFault $fault) {
			$this->error($fault);
		}
	}
	
	public function state($tid) {
		try {
			$state = $this->soap->api_status($this->login,$this->password,$tid);
		}
		catch(SoapFault $fault) {
			$this->error($fault);
		}
		if($state['retfields']['action'] == "Create") {
			if(($state['retfields']['status'] == "Wait")||($state['retfields']['status'] == "ToProcess")) {
				return 4;			
			}
			elseif($state['retfields']['status'] == "Done") {
				return 1;
			}
		}
		elseif($state['retfields']['action'] == "Update") {
			if(($state['retfields']['status'] == "Wait")||($state['retfields']['status'] == "ToProcess")) {
				return 7;			
			}
			elseif($state['retfields']['status'] == "Done") {
				return 1;
			}
		}
	
	}
}

?>
